﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Krile.Cores.KeyAssignElems;
using Krile.Kernel.WindowsEx;

namespace Krile.Forms.Dialogs.Popup
{
    public partial class Update : Form
    {
        public Update(string cvs, string nvs)
        {
            InitializeComponent();
            versionDescLabel.Text = String.Format(versionDescLabel.Text, cvs, nvs);
            if (Environment.OSVersion.Version.Major < 6)
            {
                // XP warning
                this.xpWarning.Visible = true;
                this.xpWarning2.Visible = true;
                autoUpdate.Enabled = false;
            }
        }

        private void openOfficial_Click(object sender, EventArgs e)
        {
            Subsystem.CommonOperation.CallBrowser(Define.DistUri);
        }

        private void quickBackupBtn_Click(object sender, EventArgs e)
        {
            //Quick backup
            //Backup key-assignment and tab information
            using (var tsfd = new SaveFileDialog())
            {
                tsfd.FileName = "kr_tabs.xml";
                tsfd.DefaultExt = ".xml";
                tsfd.Filter = "Xml document|*.xml";
                tsfd.Title = "Tab information file";
                if (tsfd.ShowDialog(this) == DialogResult.OK)
                {
                    try
                    {
                        Krile.Imported.Snippets.Files.SaveXML<Cores.Config._DynamicLoad.TabDataKeeper[]>(tsfd.FileName, Core.FormManager.MainForm.TabDatas);
                    }
                    catch (Exception ex)
                    {
                        Subsystem.Debugger.AddReport(ex);
                    }
                }

                using (var ksfd = new SaveFileDialog())
                {
                    ksfd.FileName = "kr_kasgn.kkd";
                    ksfd.Filter = "Krile key-assign data|*.kkd";
                    ksfd.DefaultExt = ".kkd";
                    ksfd.Title = "Save key-assign definition...";
                    if (ksfd.ShowDialog() == DialogResult.OK)
                    {
                        var sd =
                            new Krile.Forms.ConfigForm.Children.
                                KeyAssignment.KrileKeyAssignDataDefinition() { assigns = Core.Config.KeyAssign.KeyAssignData };
                        Imported.Snippets.Files.SaveXML<Krile.Forms.ConfigForm.Children.
                            KeyAssignment.KrileKeyAssignDataDefinition>(ksfd.FileName, sd); ;
                    }
                }
                MessageBoxEx.Show("OK!", "Backup completed!", MessageBoxEx.MessageBoxExButtons.OK, MessageBoxEx.MessageBoxExIcon.Information);
            }
        }

        private void Update_Load(object sender, EventArgs e)
        {
            if (Define.Unsupported)
            {
                autoUpdate.Enabled = false;
            }
        }

        private void understandXPWarning_Click(object sender, EventArgs e)
        {
            xpWarning.Visible = false;
            xpWarning2.Visible = false;
            autoUpdate.Enabled = true;
        }
    }
}
